package com.bigmodel.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaiduWenXin {

    private final static String API_KEY = "r4UzD8T06ld7RWo5QfKDeOKb";
    private final static String  SECRET_KEY = "tMawXUmwgSbKkpGAL1HwffsEDKOyPZhO";


    public static String getToken(){
        String url = "https://aip.baidubce.com/oauth/2.0/token";
        Map<String,Object> map  = new HashMap<>();
        map.put("grant_type","client_credentials");
        map.put("client_id",API_KEY);
        map.put("client_secret",SECRET_KEY);
        String response = HttpUtil.get(url, map);

        if(ObjectUtil.isNotEmpty(response)){
            return JSONUtil.parseObj(response).getStr("access_token");
        }
        return null;
    }


    public static String chat(String prompt){

        String token = getToken();
        if(ObjectUtil.isEmpty(token)){
            return "501";
        }

        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/eb-instant?access_token=" + token;

        Map<String,Object> params = new HashMap<>();
        List<Map<String,String>> messages = new ArrayList<>();
        
        Map<String,String> messageItem = new HashMap<>();
        messageItem.put("role","user");
        messageItem.put("content",prompt);
        messages.add(messageItem);
        params.put("messages",messages);

        HttpRequest reuqest = HttpUtil.createPost(url);
        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");
        reuqest.addHeaders(headers);
        reuqest.body(JSONUtil.toJsonStr(params));

        String response = reuqest.execute().body();

        if(ObjectUtil.isNotEmpty(response)){
            return JSONUtil.parseObj(response).getStr("result");
        }

        return null;
    }


    public static void main(String[] args) {
        String message = "通过分析问题，从当前问题和历史对话中提取关键词槽,完成工具参数填充,词槽只能来源于当前问题和历史对话中一部分,禁止臆想，只返回JSON：\n" +
                "\n" +
                "词槽提取按照以下原则进行\n" +
                "原则1：词槽优先来于'当前问题'\n" +
                "原则2：当前问题中没有的词槽，按照对话1,对话2,对话3的顺序进行一次查找提取\n" +
//                "原则3：如果存在时间类词槽，仔细理解后请转为时间戳\n" +
                "\n" +
                "你可以使用以下工具：\n" +
                "系统健康度分数查询: 系统健康度分数查询(systemName: str, startTime: str, endTime: str) - 查询系统的健康度分数,系统名称,起始日期,结束日期\n" +
                "\n" +
                "The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\n" +
                "```json\n" +
                "{\n" +
                "\t\"systemName\": string  // 在这里写下系统名称\n" +
                "\t\"startTime\": string  // 在这里写下起始日期\n" +
                "\t\"endTime\": string  //在这里写下结束日期\n" +
                "}\n" +
                "\n" +
                "开始提取\n" +
                "当前问题: 查询CRM系统2小时内的健康度分数\n" +
                "历史对话：\n" +
                "\t对话1：查询BOSS系统健康度\n" +
                "\t对话2：2小时内\n" +
                "\t对话3：最近5分钟健康度\n" +
                "\t对话4：查询CRM01最近5分钟";
//        String chat = BaiduWenXin.chat("你好,帮我查下中国10大热门网站");
        String chat = BaiduWenXin.chat(message);

//        System.out.println(chat);
        System.out.println(MarkdownUtil.extractJsonText(chat));
    }



}
